package com.company.batch.writer;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.client.RestTemplate;

public class RestWriter<T> implements ItemWriter<T>, InitializingBean {

	Logger logger = Logger.getLogger(RestWriter.class);

	private String urlService;
	private RestTemplate restTemplate;
	private boolean isTest = false;

	public void afterPropertiesSet() throws Exception {

	}

	public void write(List<? extends T> items) throws Exception {
		for (T t : items) {
			if (!isTest) {
				restTemplate.put(urlService, t.getClass(), t);
			}
			logger.info(" was processed "+items.size()+" items "+t.getClass());
		}
	}

	public void setUrlService(String urlService) {
		this.urlService = urlService;
	}

	public void setRestTemplate(RestTemplate restTemplate) {
		this.restTemplate = restTemplate;
	}

	public void setIsTest(boolean isTest) {
		this.isTest = isTest;
	}

}
